import re

from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By

from crm.util.loginfo import LogInfo


class BrowserOperation:

    def __init__(self, driver):
        self.driver = driver
        self.loginfo = LogInfo()

    # 打开网页
    def open_url(self, url):
        self.loginfo.set_message('info', '打开浏览器：' + url)
        self.driver.get(url)

    # 定位操作
    def find_element(self, type, locator):
        try:
            element = None
            if type.lower() == 'id':
                element = self.driver.find_element(By.ID, locator)
            elif type.lower() == 'name':
                element = self.driver.find_element(By.NAME, locator)
            elif type.lower() == 'xpath':
                element = self.driver.find_element(By.XPATH, locator)
            elif type.lower() == 'class':
                element = self.driver.find_element(By.CLASS_NAME, locator)
            elif type.lower() == 'link_text':
                element = self.driver.find_element(By.LINK_TEXT, locator)
            return element

        except Exception as e:
            self.loginfo.set_message('error','locator not find!'+str(e))

    # 清空后输入文本
    def input_text(self, type, locator, text):
        try:
            self.loginfo.set_message('info', '输入文本-' + "定位:" + locator + '输入：' + text)
            self.find_element(type, locator).clear()
            self.find_element(type, locator).send_keys(text)
        except Exception as e:
            self.loginfo.set_message('error',e)

    # 点击操作
    def click(self, type, locator):
        try:
            self.loginfo.set_message('info', '点击操作-' + "定位:" + locator)
            self.find_element(type, locator).click()
        except Exception as e:
            self.loginfo.set_message('error',e)

    # 获取文本
    def get_text(self, type, locator):
        try:
            return self.find_element(type, locator).text
        except Exception as e:
            self.loginfo.set_message('error',e)

    # 获取title
    def get_title(self, type, locator):
        try:
            return self.find_element(type, locator).title
        except Exception as e:
            self.loginfo.set_message('error',e)

    # 弹窗操作
    def alert_opertaion(self, number=''):
        alert = Alert(self.driver)
        if number == 1:
            alert.accept()  #确认
        elif number == 2:
            alert.dismiss()
        elif number == '':
            return alert.text
        else:
            print('error')

    # 获取,查询总条数
    def search_strip(self, type, locator):
        strip = self.get_text(type, locator)
        pat = '共有(.*?)条记录'
        res_strip = re.findall(pat, strip)
        int_strip = int(res_strip[0])
        return int_strip

    # 通过编号，点击修改或删除
    # def click_for_info(self,pieces,couNo,number=''):
    #     flag=0
    #     # 获取页数
    #     for k in range(pieces):
    #         # 获取每页的内容,每页展示5条
    #         for i in range(5):
    #             element = self.get_text('xpath', '/html/body/center/form'
    #                                           '/table[1]/tbody/tr[' + str(i+2) + ']/td[1]')
    #             if element == couNo:  # 如果编号满足，就点击修改
    #                 if number ==1 or number ==2:
    #                     self.click('xpath', '/html/body/center'
    #                                     '/form/table[1]/tbody/tr[' + str(i+2) + ']/td[7]/a['+str(number)+']')
    #                     flag=1
    #                     break
    #         if flag == 1:
    #             break
    #         # 翻页
    #         self.click('link_text','第'+str(k+1) +'页')
